home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13968 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mailhub.scitec.com.au!not-for-mail
  2. From: johns@rd.scitec.com.au (John Saunders)
  3. Newsgroups: comp.lang.c
  4. Subject: C syntax question
  5. Date: 11 Apr 1996 03:58:12 GMT
  6. Organization: SCITEC LIMITED, Sydney, Australia.
  7. Message-ID: <4ki00k$a4@mailhub.scitec.com.au>
  8. NNTP-Posting-Host: hydra.scitec.com.au
  9. X-Newsreader: TIN [UNIX 1.3 950515BETA PL0]
  10.  
  11. I have a question on C syntax that doesn't seem to be covered by the books
  12. that I have. Maybe somebody with the full ANSI spec. could enlighten me.
  13.  
  14. Consider the following code fragment:
  15.  
  16.     typedef unsigned char    byte;
  17.     typedef int                data;
  18.     typedef struct
  19.     {
  20.         byte        byte;
  21.         struct
  22.         {
  23.             int        dummy;
  24.         }            data;
  25.     }    struct_t;
  26.  
  27. This is supposedly correct C, typedef names are ignored when defining
  28. structure members. But not when defining variables so:
  29.     byte    byte;
  30. is an error when not inside a structure definition. Am I correct so far?
  31.  
  32. I goal is to write a parser that handles the above correctly, all example
  33. ANSI C parsers I have seen don't. I started playing around with mixing
  34. typedefs and type keywords in the same declaration to see how C compilers
  35. react. This is where it got strange. Consider the code.
  36.  
  37.     typedef unsigned char    ubyte;
  38.     typedef signed char        byte;
  39.  
  40.     ubyte signed            i;
  41.     byte unsigned            j;
  42.     signed ubyte            k;
  43.     unsigned byte            l;
  44.  
  45. The compilers I tried allowed the declaration of i and j (some gave warnings
  46. and others didn't). However none liked the declaration of k and l. From this
  47. it would seem that a typedef name is allowed only as the first token in the
  48. declaration. All the example ANSI C grammars that I have seen allow any number
  49. of typedef names or type keywords in any order. This is causing a major
  50. problem in being able to parse "byte byte;" correctly in the structure
  51. definition. I.e. is the second occurance of "byte" supposed to be treated as
  52. a typedef name or the member name?
  53.  
  54. Has anyone tackled this problem before?
  55.  
  56. Thanks.
  57. --            +------------------------------------------------------------+
  58.         .     | John Saunders  - johns@rd.scitec.com.au             (Work) |
  59.     ,--_|\    |                - john@nlc.net.au                    (Home) |
  60.    /  Oz  \   |                - http://www.nlc.net.au/~john/              |
  61.    \_,--\_/   | SCITEC LIMITED - Phone +61 2 428 9563 - Fax +61 2 428 9933 |
  62.          v    +------------------------------------------------------------+
  63.